package com.ruanjian2001.Dao;

import com.ruanjian2001.entity.User;
import com.ruanjian2001.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {

  /**
   * 添加用户
   * @param user 要添加的用户信息对象
   */
  public void addUser(User user) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
      conn = JDBCUtil.getConnection();
      String sql = "insert into user(username,password,gender," +
              "email,telephone,introduce,activecode,state) values(?,?,?,?,?,?,?,1)";
      ps = conn.prepareStatement(sql);
      ps.setString(1, user.getUsername());
      ps.setString(2, user.getPassword());
      ps.setString(3, user.getGender());
      ps.setString(4, user.getEmail());
      ps.setString(5, user.getTelephone());
      ps.setString(6, user.getIntroduce());
      ps.setString(7, user.getActiveCode());

      ps.executeUpdate();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(conn, ps, null);

    }
  }


  /**
   * 根据用户姓名查询用户姓名和密码查询信息，验证登录
   * @param name 用户名
   * @param pwd 密码
   * @return 对应的对象信息
   */
  public User findUserByUsernameAndPwd(String name, String pwd) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    User us = new User();
    try {
      conn = JDBCUtil.getConnection();
      String sql = "select * from user where username = ? and PASSWORD = ?";

      ps = conn.prepareStatement(sql);

      ps.setString(1, name);
      ps.setString(2, pwd);

      rs = ps.executeQuery();

      while (rs.next()) {
        us.setId(rs.getInt("id"));
        us.setUsername(rs.getString("username"));
        us.setPassword(rs.getString("PASSWORD"));
        us.setGender(rs.getString("gender"));
        us.setEmail(rs.getString("email"));
        us.setTelephone(rs.getString("telephone"));
        us.setIntroduce(rs.getString("introduce"));
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.close(conn, ps, rs);
    }
    return us;
  }


  /**
   * 根据用户名字查询用户
   * @param name 注册时的名字
   * @return 数据库中是否存在相同信息，如果有返回true，没有返回false
   */
   public Boolean findUser(String name){
     Connection conn = null;
     PreparedStatement ps = null;
     ResultSet rs = null;
     try{
       conn = JDBCUtil.getConnection();
       String sql = "select * from user where username = ?";
       ps = conn.prepareStatement(sql);
       ps.setString(1,name);
       rs = ps.executeQuery();


       if(rs.next() == false){
         //如果没有搜索到用户返回false
         return false;
       }
     }catch(Exception e){
       e.printStackTrace();
     }finally{
       JDBCUtil.close(conn,ps,rs);
     }

     return true;
   }


  /**
   * 修改用户信息
   * @param user 被修改的对象
   * @return 修改成功，返回true，失败返回false
   */
  public Boolean modify(User user) {

      Connection conn = null;
      PreparedStatement stmt = null;

      try {
        conn = JDBCUtil.getConnection();

        String sql = "update user set PASSWORD = ?,gender = ?,telephone = ? where username = ?";

        stmt = conn.prepareStatement(sql);

        stmt.setString(1,user.getPassword());
        stmt.setString(2,user.getGender());
        stmt.setString(3,user.getTelephone());
        stmt.setString(4,user.getUsername());

        int a = stmt.executeUpdate();

        if(a == 1){
          return true;
        }
      }catch(Exception e){
        e.printStackTrace();
      }finally {
        JDBCUtil.close(conn,stmt,null);
      }
      return false;
    }


}



